<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :xs="7" :sm="6" :md="4" :lg="4" :xl="4">
        <div class="head-container">
          <el-input v-model="deptName" clearable placeholder="输入部门名称搜索" prefix-icon="el-icon-search" style="width: 100%;" size="small" @input="getDeptDatas"/>
        </div>
        <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" default-expand-all @node-click="handleNodeClick"/>
      </el-col>
      <el-col :xs="17" :sm="18" :md="20" :lg="20" :xl="20">
        <eHeader :query="query" :sup_this="sup_this" :dicts="dicts"/>
        <!--表格渲染-->
        <el-table v-loading="loading" :data="data" style="width: 100%;">
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-form label-position="left" inline>
                <el-form-item label="用户岗位:">
                  <span v-for="(item, index) in scope.row.roles" :key="index" style="color: #909399; margin-right: 10px">
                    {{ item.nickname }}  
                  </span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="userName" label="用户名"/>
          <el-table-column prop="realname" label="姓名"/>
          <el-table-column prop="telephone" label="电话"/>
          <el-table-column :show-overflow-tooltip="true" prop="email" label="邮箱"  min-width="120px"/>
          <el-table-column label="部门">
            <template slot-scope="scope">
              <div>{{ scope.row.dept.name || '' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="stateTxt" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.state == 1" type="plain">{{scope.row.stateTxt}}</el-tag>
              <el-tag v-if="scope.row.state == 2" type="danger">{{scope.row.stateTxt}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="账号类型" prop="reserve" align="center">
            <template slot-scope="scope" v-if="scope.row.reserve == 1">
              预留账号
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="lastLoginAt" label="上次登录时间">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.lastLoginAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="checkPermission(['ADMIN','USER_ALL','USER_EDIT','USER_DELETE'])" label="操作" width="125" align="center">
            <template slot-scope="scope">
              <edit v-permission="['ADMIN','USER_ALL','USER_EDIT']" :dicts="dicts" :data="scope.row" :sup_this="sup_this"/>
              <em class="block-col"/>
              <el-popover
                v-permission="['ADMIN','USER_ALL','USER_DELETE']"
                :ref="scope.row.id"
                placement="top"
                width="180">
                <p v-if="scope.row.state == 1">确定停用该账号吗？</p>
                <p v-if="scope.row.state == 2">确定启用该账号吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button :loading="delLoading" type="primary" size="mini" @click="subConfirm(scope.row.id, scope.row.state)">确定</el-button>
                </div>
                <el-button slot="reference" type="text" v-if="scope.row.state == 1">停用</el-button>
                <el-button slot="reference" type="text" v-if="scope.row.state == 2">启用</el-button>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        
        <!--分页组件-->
        <div class="pg-wrapper">
          <el-pagination
            :total="total"
            layout="total, prev, pager, next, sizes, jumper"
            @size-change="sizeChange"
            @current-change="pageChange"/>
        </div>
        
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { forbidden, enable } from '@/api/user'
import { getDepts } from '@/api/dept'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
export default {
  components: { eHeader, edit },
  mixins: [initData, initDict],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      delLoading: false,
      sup_this: this,
      deptName: '',
      depts: [],
      deptId: null,
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDeptDatas()
    this.$nextTick(() => {
      this.init()
      // 加载数据字典
      // this.getDict('user_status')
      this.dicts = [
        { value: 1, label: '正常' },
        { value: 0, label: '停用' }
      ]
    })
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'account/list'
      const sort = 'id,desc'
      const query = this.query
      const username = query.username
      const type = query.type
      const value = query.value
      const enabled = query.enabled
      this.params = { page: this.page, size: this.size, username: username, sort: sort, deptId: this.deptId }
      if (type && value) { this.params[type] = value }
      if (enabled !== '' && enabled !== null) { this.params['enabled'] = enabled }
      return true
    },
    subConfirm(id, state) {
      if (state == 1) {
        this.subDelete(id)
      } else {
        this.subEnable(id)
      }
    },
    subEnable(id) {
      this.delLoading = true
      enable(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        if (res.code == 0) {
          this.$notify({
            title: '账号启用成功',
            type: 'success',
            duration: 2500
          })
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.msg)
      })
    },
    subDelete(id) {
      this.delLoading = true
      forbidden(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        if (res.code == 0) {
          this.$notify({
            title: '账号停用成功',
            type: 'success',
            duration: 2500
          })
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.msg)
      })
    },
    getDeptDatas() {
      const sort = 'id,desc'
      const params = { sort: sort }
      if (this.deptName) { params['name'] = this.deptName }
      getDepts(params).then(res => {
        this.depts = res.data.list
      })
    },
    handleNodeClick(data) {
      if (data.pid === 0) {
        this.deptId = null
      } else {
        this.deptId = data.id
      }
      this.init()
    }
  }
}
</script>

<style scoped>
</style>
